#include "mainwindow.h"
#include "ui_mainwindow.h"

#define PORT_SIZE_MAX_DL8000	44
#define PORT_DATA_MAX_DL8000	(PORT_SIZE_MAX_DL8000-12)
//#define DEBUG_DL8000

void MainWindow::Port_dl8000_Port_Init()
{
    //
    Port_dl8000 = new QSerialPort();
    Thread_dl8000 = new QThread();
    connect(Thread_dl8000, &QThread::finished, this,&QObject::deleteLater);
    Thread_dl8000->start();
    Port_dl8000->moveToThread(Thread_dl8000);
    connect(Port_dl8000, &QSerialPort::readyRead, this, &MainWindow::Port_dl8000_Serial_Read_clicked);

    SyncTimer_dl8000 = new QTimer;
    connect(SyncTimer_dl8000,&QTimer::timeout,this,&MainWindow::Port_dl8000_Sync_Timer);//同步
    SyncTimer_dl8000->stop();
    RecvTimer_dl8000 = new QTimer;
    connect(RecvTimer_dl8000,&QTimer::timeout,this,&MainWindow::Port_dl8000_Serial_Read_Data);//
    RecvTimer_dl8000->stop();
    WaitTimer_dl8000 = new QTimer(this);
    connect(WaitTimer_dl8000,&QTimer::timeout,this,&MainWindow::Port_dl8000_Once_Timer);
    WaitTimer_dl8000->setSingleShot(true);
}

// 写盖板码
bool MainWindow::Port_dl8000_Write_Mtsn(int SendCmd)
{
    QString MtsnStr = ui->te_mtsn_dl8000->toPlainText();
    if(MtsnStr.isEmpty())
    {
        if(LangureStu == 1)
        {
            LangureStr = "MtSN burning Failed, Burn MtSN cannot be empty!";
        }
        else
        {
            LangureStr = "盖板码写入失败，写入盖板码不能为空!";
        }
		InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        return false;
    }
    else if(44 != MtsnStr.size())
    {
        if(LangureStu == 1)
        {
            LangureStr = tr("MtSN burning Failed, MtSN length[%1] is incorrect, Pleasere-enter it!").arg(MtsnStr.size());
        }
        else
        {
            LangureStr = QString("盖板码写入失败，盖板码长度[%1]有误，请重新输入!").arg(MtsnStr.size());
        }
        InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        return false;
    }

    //发送写入盖板码请求
    QByteArray LineStr;
    LineStr.clear();
    LineStr = MtsnStr.toLatin1();
#if 0
	if(SEND_BURN_MTSN_DEV == SendCmd)
	{
		LangureStr_Init_BurningCode_DevData(ui->te_prin_dl8000);
	}
	else
	{
		LangureStr_Init_BurningCode(ui->te_prin_dl8000);
	}
#else
	LangureStr_Init_BurningCode(ui->te_prin_dl8000);
#endif

    Port_dl8000_Send_3Time(LineStr,PORT_STU_BURN_MTSN,SendCmd);
    if(PORT_STU_BURN_MTSN != PortStu_dl8000)
    {
        if(SEND_BURN_MTSN_DEV == SendCmd)
        {
            if(PORT_STU_BUSY == PortStu_dl8000)
            {
                LangureStr_Init_BurnCode_DevData_Failed_Busy(ui->te_prin_dl8000);
            }
            else if(PORT_STU_TIMEOUT == PortStu_dl8000)
            {
                LangureStr_Init_BurnCode_DevData_Failed_Time(ui->te_prin_dl8000);
            }
            else
            {
                LangureStr_Init_BurnCode_DevData_Failed(ui->te_prin_dl8000);
            }
        }
        else
        {
            if(PORT_STU_BUSY == PortStu_dl8000)
            {
                LangureStr_Init_BurnCode_Failed_Busy(ui->te_prin_dl8000);
            }
            else if(PORT_STU_TIMEOUT == PortStu_dl8000)
            {
                LangureStr_Init_BurnCode_Failed_Time(ui->te_prin_dl8000);
            }
            else
            {
                LangureStr_Init_BurnCode_Failed(ui->te_prin_dl8000);
            }
        }
        return false;
    }

    //等待mcu回应
    if(!Port_dl8000_Wait_Answer())
    {
        if(PORT_STU_BUSY == PortStu_dl8000)
        {
            LangureStr_Init_BurnCode_DevData_Failed_Busy(ui->te_prin_dl8000);
        }
        else if(PORT_STU_TIMEOUT == PortStu_dl8000)
        {
            LangureStr_Init_BurnCode_Failed_Time(ui->te_prin_dl8000);
        }
        else
        {
            LangureStr_Init_BurnCode_Failed(ui->te_prin_dl8000);
        }
        return false;
    }

#if 0
    if(SEND_BURN_MTSN_DEV == SendCmd && PORT_STU_NULL != PortStu_dl8000)
    {
        //替换底层
        QString DevFileName = FilePath + "/";
        switch (ModelStu) {
        case Model_8:
        {
            DevFileName += ROM_FILE_NAME_I8;
            break;
        }
        case Model_8P:
        {
            DevFileName += ROM_FILE_NAME_I8P;
            break;
        }
        case Model_8P_a:
        {
            DevFileName += ROM_FILE_NAME_I8P_A;
            break;
        }
        case Model_X:
        {
            DevFileName += ROM_FILE_NAME_IX;
            break;
        }
        case Model_XR:
        {
            DevFileName += ROM_FILE_NAME_IXR;
            break;
        }
        case Model_XSMax:
        {
            DevFileName += ROM_FILE_NAME_IXSMAX;
            break;
        }
        case Model_11:
        {
            DevFileName += ROM_FILE_NAME_I11;
            break;
        }
        case Model_11P:
        {
            DevFileName += ROM_FILE_NAME_I11P;
            break;
        }
        case Model_12:
        case Model_12PM:
        {
            DevFileName += ROM_FILE_NAME_I12;
            break;
        }
        case Model_13:
        {
            DevFileName += ROM_FILE_NAME_I13;
            break;
        }
        case Model_15:
        case Model_14:
        {
            DevFileName += ROM_FILE_NAME_I14;
            break;
        }
        case Model_13P:
        {
            DevFileName += ROM_FILE_NAME_I13Pro;
            break;
        }
        case Model_15P:
        case Model_14P:
        {
            DevFileName += ROM_FILE_NAME_I14Pro;
            break;
        }
        }
        QFile DevFile(DevFileName);
        bool exist = QFile::exists(DevFileName);
        if(!exist || 16384 != DevFile.size())
        {
            if(LangureStu == 1)
            {
                LangureStr = "MtSN burning Failed, Corrupt underlying file!";
            }
            else
            {
                LangureStr = "盖板码写入失败，底层文件损坏!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
            return false;
        }
        if(!DevFile.open(QIODevice::ReadOnly))
        {
            if(LangureStu == 1)
            {
                LangureStr = "MtSN burning Failed, Failed to open the underlying file!";
            }
            else
            {
                LangureStr = "盖板码写入失败，底层文件打开失败!";
            }
            InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
            return false;
        }

        char LineBuff[PORT_DATA_MAX_DL8000];
        qint64 Length;
        int BurnCnt = 0;
        int DevSize = DevFile.size();
        int FileSize = 0;
        PageCnt_dl8000 = 0;
        while(!DevFile.atEnd()){
            if(DevSize - FileSize > PORT_DATA_MAX_DL8000)
            {
                Length = DevFile.read(LineBuff, PORT_DATA_MAX_DL8000);
            }
            else
            {
                Length = DevSize - FileSize;
                Length = DevFile.read(LineBuff, Length);
            }
            QByteArray BurnBuff;
            BurnBuff.clear();
            for(int i=0;i<Length;i++)
            {
                BurnBuff.append(LineBuff[i]);
            }
            if(0 != Length)
            {
                Port_dl8000_Send_3Time(BurnBuff, PORT_STU_BURN,SEND_BURN_DATA);

                if(PORT_STU_BURN != PortStu_dl8000)
                {
                    if(PORT_STU_BUSY == PortStu_dl8000)
                    {
                        LangureStr_Init_BurnCode_DevData_Failed_Busy(ui->te_prin_dl8000);
                    }
                    else if(PORT_STU_TIMEOUT == PortStu_dl8000)
                    {
                        LangureStr_Init_BurnCode_DevData_Failed_Time(ui->te_prin_dl8000);
                    }
                    else
                    {
                        LangureStr_Init_BurnCode_DevData_Failed(ui->te_prin_dl8000);
                    }
                    DevFile.close();
                    ui->progressBar_dl8000->setValue(0);
                    return false;
                }
                BurnCnt++;
                FileSize += Length;
                ui->progressBar_dl8000->setValue((int)(1+98*BurnCnt/32));
                if(32 == BurnCnt)
                {
                    break;
                }
            }
        }
        DevFile.close();

        LastPortStu_dl8000 = PORT_STU_FREE;
        PortStu_dl8000 = PORT_STU_FREE;

        //等待mcu回应
		if(!Port_dl8000_Wait_Answer())
		{
			if(PORT_STU_BUSY == PortStu_dl8000)
			{
				LangureStr_Init_BurnCode_DevData_Failed_Busy(ui->te_prin_dl8000);
			}
			else if(PORT_STU_TIMEOUT == PortStu_dl8000)
			{
				LangureStr_Init_BurnCode_DevData_Failed_Time(ui->te_prin_dl8000);
			}
			else
			{
				LangureStr_Init_BurnCode_DevData_Failed(ui->te_prin_dl8000);
			}
			return false;
		}
    }
#endif
    if(PORT_STU_ERROR == PortStu_dl8000)
    {
        if(SEND_BURN_MTSN_DEV == SendCmd)
        {
            LangureStr_Init_BurnCode_DevData_Failed(ui->te_prin_dl8000);
        }
        else
        {
            LangureStr_Init_BurnCode_Failed(ui->te_prin_dl8000);
        }
        return false;
    }

    return true;
}

// 命令发送
void MainWindow::Port_dl8000_Send_Data(QByteArray LineStr,int SendStu)
{
    QByteArray LineBuff = LineStr;
    QByteArray SendBuff;
    SendBuff.resize(12);
    int BuffCnt = 0;

    QByteArray RandNums;
    RandNums.resize(4);
    RandNums.clear();
    generateRandomNumber(RandNums, 4);
    for(int icnt=0;icnt<4;icnt++)
    {
        SendBuff[icnt] = RandNums.at(icnt);
    }
    BuffCnt += 4;
    int TempModel;
    TempModel = ModelStu_dl8000;
    //id
    switch (SendStu) {
    case SEND_READ_MTSN_REQ:
    {
        SendBuff[BuffCnt] = 0x00;
        SendBuff[BuffCnt+1] = 0xFF;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0xA1;
        break;
    }
    case SEND_BURN_MTSN_REQ:
    {
        SendBuff[BuffCnt] = 0x00;
        SendBuff[BuffCnt+1] = 0xFF;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0xB1;
        break;
    }
    case SEND_DATA_UPG:
    {
        PageCnt_dl8000++;
        SendBuff[BuffCnt] = 0x00;
        SendBuff[BuffCnt+1] = (PageCnt_dl8000&0x00FF);
        SendBuff[BuffCnt+2] = ((PageCnt_dl8000>>8)&0x00FF);
        SendBuff[BuffCnt+3] = 0xC0;
        break;
    }
    case SEND_FILE_SIZE:
    {
        SendBuff[BuffCnt] = (AllFileSize_dl8000&0x00FF);
        SendBuff[BuffCnt+1] = ((AllFileSize_dl8000>>8)&0x00FF);
        SendBuff[BuffCnt+2] = ((AllFileSize_dl8000>>16)&0x00FF);
        SendBuff[BuffCnt+3] = 0xC2;
        break;
    }
    case SEND_DATA_DONE:
    {
        SendBuff[BuffCnt] = 0xFF;
        SendBuff[BuffCnt+1] = 0xFF;
        SendBuff[BuffCnt+2] = 0xFF;
        SendBuff[BuffCnt+3] = 0xC0;
        break;
    }
    default:
    {
        SendBuff[BuffCnt] = 0x01;
        SendBuff[BuffCnt+1] = 0x00;
        SendBuff[BuffCnt+2] = 0x00;
        SendBuff[BuffCnt+3] = 0x00;
        break;
    }
    }
    BuffCnt += 4;
    //Length
    uint16_t Size;
    Size = LineStr.size();
    SendBuff[BuffCnt+1] = (Size >> 8) & 0x00FF;
    SendBuff[BuffCnt] = Size & 0x00FF;
    BuffCnt += 2;
    //check add
    uint16_t AddVal;
    AddVal = crc16_result(SendBuff, 4);
    BuffCnt += 2;
    // 数据
    int icnt;
    if(!LineStr.isEmpty())
    {
        for(icnt=0;icnt<LineStr.size();icnt++) {
            SendBuff.append(LineBuff.at(icnt));
        }
        BuffCnt += icnt;
        AddVal += crc16_result(SendBuff.mid(12,Size), Size);
    }
    SendBuff[11] = (AddVal >> 8) & 0x00FF;
    SendBuff[10] = AddVal & 0x00FF;

    SendData_dl8000 = SendBuff;
    Port_dl8000->write(SendBuff, SendBuff.size());
}

// 升级
bool MainWindow::Port_dl8000_Upgrade_File()
{
    //升级提示类型
    // 0.无升级文件
    // 1.软件升级
    int UpgTipType = 0;
    //升级文件
    QString VerFileName = FilePath + "/" + VersName_dl8000;
    QFile VerFile(VerFileName);
#ifdef DEBUG_DL8000
    if(!VerFile.open(QIODevice::ReadOnly))
    {
        VerFile.close();
    }
    QByteArray AllData = VerFile.readAll();
    int AddVal;
    if(!AllData.isEmpty())
    {
    //    LipsCrc32_Calc(AllData, AllData.size());
        LipsCrc32_Calc(AllData, 0x9000);
    }
    VerFile.close();
    AddVal = Crc32;
    int Valsize = DatLen;
#endif
    ui->progressBar_dl8000->setValue(0);
    char LineBuff[PORT_DATA_MAX_DL8000];
    qint64 Length;
    int VerSize = VerFile.size();
    int ToalFileSize = VerSize;
    if(0 >= ToalFileSize)
    {
        if(LangureStu == 1)
        {
            LangureStr = "No upgrade file";
        }
        else
        {
            LangureStr = "无升级文件";
        }
        InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    }
    else
    {
        LangureStr = QString("升级文件大小:%1").arg(VerSize);
        InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    }
	int FileSize = 0;
	bool DoneStu = true;
    PageCnt_dl8000 = 0;

	QByteArray LineStr;
	LineStr.clear();
    // 发送文件总大小
    AllFileSize_dl8000 = VerSize;
	Port_dl8000_Send_Data(LineStr, SEND_FILE_SIZE);
	Port_dl8000_Wait_Time(TIME_MCU_TIME_OUT);

	DoneStu = true;
	if(VerSize == 0)
	{
		DoneStu = false;
	}
	else if(!VerFile.open(QIODevice::ReadOnly))
	{
		VerFile.close();
	}
	else if(PORT_STU_NULL != PortStu_dl8000)
	{
		UpgTipType += 1;

		while(!VerFile.atEnd()){
			if(VerSize - FileSize > PORT_DATA_MAX_DL8000)
			{
				Length = VerFile.read(LineBuff, PORT_DATA_MAX_DL8000);
			}
			else
			{
				Length = VerSize - FileSize;
				Length = VerFile.read(LineBuff, Length);
			}
			QByteArray VersBuff;
			VersBuff.clear();
			for(int i=0;i<Length;i++)
			{
				VersBuff.append(LineBuff[i]);
			}
			if(0 != Length)
			{
				Port_dl8000_Send_3Time(VersBuff, PORT_STU_UPG, SEND_DATA_UPG);
				if(PORT_STU_UPG != PortStu_dl8000)
				{
					ui->progressBar_dl8000->setValue(0);
					if(PORT_STU_BUSY == PortStu_dl8000)
					{
						if(LangureStu == 1)
						{
							LangureStr = "The device is busy, please operate later!";
						}
						else
						{
							LangureStr = "设备繁忙，请稍后操作!";
						}
						InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
					}
					else if(PORT_STU_TIMEOUT == PortStu_dl8000)
					{
						if(LangureStu == 1)
						{
							LangureStr = "The device timed out. Please check the device connection status and try again!";
						}
						else
						{
							LangureStr = "设备超时，请检查设备连接状态后重试!";
						}
						InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
					}
					else
                    {
                        if(LangureStu == 1)
                        {
                            LangureStr = "Upgrade failed!";
                        }
                        else
                        {
                            LangureStr = "升级失败!";
                        }
						InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
                    }
					VerFile.close();
					return false;
				}
			}
			FileSize += Length;
        #ifdef DEBUG_DL8000
          LangureStr = QString("Upgrade %1 Page.").arg(PageCnt_dl8000);
          InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
        #endif
            ui->progressBar_dl8000->setValue((int)(99*FileSize/VerSize));
		}
		VerFile.close();

        if(DoneStu == true)
        {
            QByteArray DoneBuff;
            DoneBuff.clear();
            Port_dl8000_Send_3Time(DoneBuff, PORT_STU_UPG, SEND_DATA_DONE);
            if(PORT_STU_ERROR == PortStu_dl8000)
            {
                return false;
            }
        }
	}
	if(PORT_STU_NULL != PortStu_dl8000)
	{
		ui->progressBar_dl8000->setValue(100);

		switch(UpgTipType) {
		//仅设备升级
		case 1:
		{
			if(LangureStu == 1)
			{
				LangureStr = "Software data transmission is completed\n"
							 "\tPlease disconnect the device and reconnect for device upgrade\n"
							 "\tWait for the equipment upgrade to complete";
			}
			else
			{
				LangureStr = "软件数据传输完成\n"
							 "\t请断开设备，并重新连接进行设备升级\n"
							 "\t等待设备升级完成";
			}
			InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_SUCCESS);
			break;
		}
		//默认
		default:
		{
			if(LangureStu == 1)
			{
				LangureStr = "No upgrade file";
			}
			else
			{
				LangureStr = "无升级文件";
			}
			InsertText(ui->te_prin_dl8000, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
			break;
		}
		}

		PortStu_dl8000 = PORT_STU_FREE;
	}
	else
	{
		return false;
	}
	LastPortStu_dl8000 = PORT_STU_FREE;
	
	return true;
}



//重发指令
void MainWindow::Port_dl8000_Serial_Cmd_Repeat()
{
    QByteArray LineStr;
    LineStr.clear();
    pc_to_mcu_send_data(LineStr, PORT_STU_REPT);
}

//数据接收:触发中断->延时后接收数据
void MainWindow::Port_dl8000_Serial_Read_clicked()
{
    RecvTimer_dl8000->start(TIMER_RECV_TIME_OUT);
}
void MainWindow::Port_dl8000_Serial_Read_Data()
{
    RecvTimer_dl8000->stop();
    RecvStu_dl8000 = false;
    QByteArray ReadBuff;
    //从接收缓冲区中读取数据
    ReadBuff.clear();
    ReadBuff = Port_dl8000->readAll();
    RecvStr_dl8000.append(ReadBuff);
	if(12 <= RecvStr_dl8000.size())
	{
        ReadBuff.clear();
        ReadBuff.append(RecvStr_dl8000);

        //校验CRC，若不成功则发送重复请求
        uint16_t AddVal;
        uint16_t AddCheckVal;
        AddVal = crc16_result(ReadBuff.mid(0,4), 4);
        //取长度
        AddCheckVal = (((ReadBuff[9]&0x00FF)<<8) & 0xFF00) | (ReadBuff[8]&0x00FF);
        int len = AddCheckVal;
        if(0 != len)
        {
            AddVal += crc16_result(ReadBuff.mid(12,len), len);
        }
        AddCheckVal = ((ReadBuff[11]<<8) & 0xFF00) | (ReadBuff[10]&0x00FF);
        if(AddVal != AddCheckVal)
        {
            RecvStu_dl8000 = true;
            RecvStr_dl8000.clear();
        }
        else
        {
            uint32_t CmdId;
            CmdId = ((ReadBuff[7] << 24)&0xFF000000) | ((ReadBuff[6] << 16)&0x00FF0000) | ((ReadBuff[5] << 8)&0x0000FF00) | ((ReadBuff[4] << 0)&0x000000FF);
            if(0xE0000000 == CmdId)
            {
                RecvData_dl8000 = ReadBuff.mid(12,len);
                if(len>0)
                {
                    LangureStr = tr("失败,%1.").arg(ByteArray2QString(RecvData_dl8000,len));
                }
                else
                {
                    if(LangureStu == 1)
                    {
                        LangureStr = "Error,unknown error!";
                    }
                    else
                    {
                        LangureStr = "失败,未知错误!";
                    }
                }
                PortStu_dl8000 = PORT_STU_ERROR;
            }
            else if(ReadBuff[7].operator==(0x00))
            {
                if(0x000A0000 == CmdId) {
                    Port_dl8000->write(SendData_dl8000, SendData_dl8000.size());
                    WaitTimer_dl8000->stop();
                    PortStu_dl8000 = PORT_STU_WAIT;
                    WaitTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
                }
                else if(0x000B0000 == CmdId)
                {
                    if(WaitTimer_dl8000->isActive())
                    {
                        WaitTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
                        SendOutCnt_dl8000 = 0;
                    }
                    else if(SyncTimer_dl8000->isActive())
                    {
                        SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
                        TimeOutCnt_dl8000 = 0;
                    }

                }
                //同步：1.端口连接后，检测数据是否为同步，同步后设置为同步状态（仅可进行界面操作）
                //校验：2.检查当前路径下是否有版本文件，若没有则报错 无法校验，请将版本文件存放到程序目录
                //校验完成：3.同步后，mcu发送当前版本号给pc，若pc校验mcu版本落后，则升级版本，此时pc设置为校验通过状态（可进行功能性操作）
                else if(CmdId == CMD_MCU_PC_SYNC) {
                    if(PORT_STU_NULL == LastPortStu_dl8000)
                    {
                        //取版本
                        QString ReadData = ByteArray2QString(ReadBuff, ReadBuff.size());
                        DevVer_dl8000.clear();
                        DevVer_dl8000.append(ReadData.mid(18,6).toLatin1());
                        //
                        if(ReadData[14] == '6')
                        {
                            VersName_dl8000 = UPG_FILE_APP_DL6000;
                            DevsName_dl8000 = "DL6000";
                        }
                        else
                        {
                            VersName_dl8000 = UPG_FILE_APP_DL8000;
                            DevsName_dl8000 = "DL8000";
                        }
                        Lbl_Ver_dl8000_Langure_to_Change();
                        BinVer_dl8000 = Get_Version_From_File(VersName_dl8000, ui->te_prin_dl8000, 0x00006000);
                        //
                        PortStu_dl8000 = PORT_STU_FREE;
                        LastPortStu_dl8000 = PortStu_dl8000;
                        if(LangureStu == 1)
                        {
                            LangureStr = DevsName_dl8000 + " Device connected";
                        }
                        else
                        {
                            LangureStr = DevsName_dl8000 + " 设备已连接";
                        }
                        InsertText(ui->te_prin_dl8000, LangureStr, 9, APP_FONT_COLOR_NORMAL);

                        //
                        if((BinVer_dl8000.size() - DevVer_dl8000.size()) <= 1)
                        {
                            if(BinVer_dl8000 == DevVer_dl8000)
                            {
                                //标记同步完成状态
                                if(LangureStu == 1)
                                {
                                    LangureStr = "You can go to the official website or contact customer service to determine whether the assistant is the latest version";
                                }
                                else
                                {
                                    LangureStr = "可前往官方网站或联系客服确定助手是否为最新版本";
                                }
                                InsertText(ui->te_prin_dl8000, LangureStr, 9, APP_FONT_COLOR_NORMAL);
                            }
                            else
                            {
                                if(BinVer_dl8000 > DevVer_dl8000)
                                {
                                    LangureStr_Init_DevVers_Low(ui->te_prin_dl8000, BinVer_dl8000);
                                }
                                else
                                {
                                    //pc端落后，提示升级
                                    LangureStr_Init_AppVers_Low(ui->te_prin_dl8000);
                                }
                            }
                        }
                        else if(BinVer_dl8000.size() > DevVer_dl8000.size())
                        {
                            //mcu版本落后
                            LangureStr_Init_DevVers_Low(ui->te_prin_dl8000, BinVer_dl8000);
                        }
                        else
                        {
                            //pc端落后，提示升级
                            LangureStr_Init_AppVers_Low(ui->te_prin_dl8000);
                        }
                        //
                        if(PORT_STU_NULL != PortStu_dl8000)
                        {
                            Port_dl8000_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
                            SyncTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
                        }
                    }
                    //MCU返回盖板码
                    else if(PORT_STU_READ_MTSN == LastPortStu_dl8000)
                    {
                        RecvStr_dl8000.clear();
                        return;
                    }
                    else if(PORT_STU_READ == LastPortStu_dl8000 && len != 512)
                    {
                        RecvStr_dl8000.clear();
                        return;
                    }
                    //操作后的同步帧
                    else if(PORT_STU_WAIT == PortStu_dl8000)
                    {
                        if(PORT_STU_WAIT_OK == LastPortStu_dl8000)
                        {
                            RecvStr_dl8000.clear();
                            return;
                        }
                        else
                        {
                            PortStu_dl8000 = LastPortStu_dl8000;
                        }
                    }
                }
                else {
                    if(PORT_STU_WAIT == PortStu_dl8000)
                    {
                        if(3 == WaitOutCnt_dl8000)
                        {
                            PortStu_dl8000 = LastPortStu_dl8000;
                            WaitOutCnt_dl8000 = 0;
                        }
                        else
                        {
                            Port_dl8000_Serial_Cmd_Repeat();
                            WaitOutCnt_dl8000++;
                            RecvStr_dl8000.clear();
                            WaitTimer_dl8000->stop();
                            PortStu_dl8000 = PORT_STU_WAIT;
                            WaitTimer_dl8000->start(TIMER_SYNC_TIME_OUT);
                            return;
                        }
                    }
                }
                RecvCnt_dl8000 = ((ReadBuff[6]<<8)&0x000000FF) | (ReadBuff[5]&0x0000FF00);
                RecvSize_dl8000 = len;
                for (int cnt=0;cnt<len;cnt++) {
                    RecvData_dl8000.append(ReadBuff.at(12+cnt));
                }
            }
            else
            {
                //读取
                if((ReadBuff[7].operator==(0xA0)) && (ReadBuff[4].operator==(0x00)))
                {
                    RecvCnt_dl8000 = ((ReadBuff[6]<<8)&0x000000FF) | (ReadBuff[5]&0x0000FF00);
                    RecvSize_dl8000 = len;
                    for (int cnt=0;cnt<len;cnt++) {
                        RecvData_dl8000.append(ReadBuff.at(12+cnt));
                    }
                    PortStu_dl8000 = LastPortStu_dl8000;
                }
                //读取盖板码成功
                else if(CmdId == 0xA2000000)
                {
                    RecvMtSN_dl8000.clear();
                    RecvMtSN_dl8000.append(ReadBuff.mid(12,len));
                    PortStu_dl8000 = LastPortStu_dl8000;
                }
                //设备读取完成
                else if(CmdId == 0xA3000000)
                {
                    PortStu_dl8000 = LastPortStu_dl8000;
                }
                //设备写入完成
                else if(CmdId == 0xB3000000)
                {
                    PortStu_dl8000 = LastPortStu_dl8000;
                }
                //设备升级完成
                else if(CmdId == 0xC3000000)
                {
                    PortStu_dl8000 = LastPortStu_dl8000;
                }
                else
                {
                    ;
                }
            }
            WaitOutCnt_dl8000 = 0;
            RecvStr_dl8000.clear();
        }
    }
    else if(PORT_STU_SYNC == PortStu_dl8000)
    {
        RecvStu_dl8000 = true;
        RecvStr_dl8000.clear();
    }
}


